package cn.com.fattiger.framework.cms.admin.common.web;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.fattiger.framework.cms.admin.common.model.Questionnaire;
import cn.com.fattiger.framework.cms.admin.common.service.QuestionnaireService;
import cn.com.fattiger.framework.cms.admin.common.service.TemplateService;
import cn.com.fattiger.framework.cms.config.CustomConfig;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * @author fattiger.xiaoyang
 * @date 2016/06/08
 */

@Controller
@RequestMapping("questionnaire")
public class WJQuestionnaireController {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private QuestionnaireService questionnaireService;

	@Autowired
	private TemplateService templateService;
	
	@Autowired
	private CustomConfig customConfig;
	
	@RequestMapping("list.do")
	public String getList(Integer page, Integer pageSize, ModelMap map){
		if(page == null || page < 0){
			page = 0;
		}
		if(pageSize == null || pageSize < 0 || pageSize > 100){
			pageSize = 10;
		}
		Page<Questionnaire> pageList = questionnaireService.list(page, pageSize);
		map.put("lists", 		pageList.getContent());
    	map.put("totalPageNum", pageList.getTotalPages());
    	map.put("currentPage", 	pageList.getNumber());
    	map.put("totalNum", 	pageList.getTotalElements());
		return "questionnaire/questionnaire_list";
	}
	
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public String save(Questionnaire entity){
		Long id = entity.getId();
		if(id == null){
			entity.setCreatetime(new Date());
			questionnaireService.save(entity);
		}else{
			Questionnaire questionnaire = questionnaireService.get(id);
			questionnaire.setDiscribe(entity.getDiscribe());
			questionnaire.setState(entity.getState());
			questionnaire.setTitle(entity.getTitle());
			questionnaire.setRemark(entity.getRemark());
			questionnaireService.save(questionnaire);
		}
		return "redirect:list.do";
	}
	
	@RequestMapping(value = "del.do", method = RequestMethod.GET)
	public String del(Long id, Integer page, ModelMap map){
		if(id != null){
			questionnaireService.delete(id);
		}
		return "redirect:list.do?page=" + page;
	}
	
	@RequestMapping(value = "edit.do", method = RequestMethod.GET)
	public String edit(Long id, Integer page, ModelMap map){
		Questionnaire entity = null;
		if(id != null){
			entity = questionnaireService.get(id);
		}
		map.put("entity", entity);
		map.put("page", page);
		return "questionnaire/questionnaire_edit";
	}
	
	@RequestMapping(value = "createHtml.do")
	@ResponseBody
	public String createHtml(Long id){
		Questionnaire entity = questionnaireService.get(id);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("entity", entity);
		String json = null;
		try {
			ObjectMapper mapper = new ObjectMapper();
			json = mapper.writeValueAsString(entity);
		} catch (JsonProcessingException e) {
			logger.error(e.getMessage(),e);
		}
		logger.info(json);
		try {
			templateService.templateToFile("questionnaire/questionnaire.ftl", data, customConfig.getStaticRoot() +  "/" + "questionnaire.html");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "questionnaire.html create success.";
	}
}
